package com.csun.nusing.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FamilyOlderListDateJsonBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean.NursingOldrestDataBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean.NursingOldrestDataBean.NursingOldrestListBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.ui.myview.RoundImageView;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.Map;

/**
 * 随访仪老人选择 ClassName: FollowOldSelectActivity
 *
 * @author fzq
 * @Description: TODO
 * @date 2017-11-3
 */
public class FollowOldSelectActivity extends BaseActivity<CommonPresenter> {

    private ListView lv;
    private String authorization;
    private String userid;
    protected NursingOldrestListJsonBean nursingoldrestlistbean;
    protected TextView nametv;
    protected TextView phonetv;
    protected Intent intent;
    private String deviceId;
    private String deviceMacAddress;
    private boolean isFamily;
    private RelativeLayout toprl;
    private FamilyOlderListDateJsonBean familyoldrestlistbean;

    @Override
    public int getContentViewId() {
        return R.layout.activity_follow_olderselet_lv;
    }

    @Override
    public void initSubViews(View view) {
        super.initSubViews(view);
        initView();
    }

    /**
     * 初始化view
     *
     * @param
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-11-3
     */
    private void initView() {
        lv = findViewById(R.id.activity_nursing_follow_oldrest_lv);
        toprl = findViewById(R.id.follow_olders_rl);
    }

    @Override
    public void initData() {
        super.initData();
        // 请求老人列表
        authorization = (String) SPUtils.get(this, "authorization", "");
        userid = (String) SPUtils.get(this, "userId", "");
        deviceId = getIntent().getStringExtra("deviceId");
        deviceMacAddress = getIntent().getStringExtra("deviceMacAddress");
        isFamily = getIntent().getBooleanExtra("isFamily", false);
        if (isFamily) {
            toprl.setBackgroundResource(R.drawable.main_whiteyello_color_shape);
            authorization = (String) SPUtils.get(this, "authorization", "");
            userid = (String) SPUtils.get(this, "userId", "");
            Map<String, String> param = new HashMap<String, String>();
            OkHttpTask.newInstance(this).post(param, OkType.form)
                    .addHeader("authorization", authorization)
                    .enqueue(new IOkTaskCallback() {
                        @Override
                        public void onSuccess(String result) {
                            if (result != null) {
                                Log.e("fzq", "-------" + result);
                                Gson gson = new Gson();
                                familyoldrestlistbean = gson.fromJson(result,
                                        FamilyOlderListDateJsonBean.class);
                                if (familyoldrestlistbean != null) {
                                    if (familyoldrestlistbean.getData() != null) {
                                        controlfamilylistView();
                                    }
                                }
                            }
                        }
                    }).start(UrlBean.FAMILYOLDERLISTURL);
        } else {
            Map<String, String> param = new HashMap<String, String>();
            param.put("userid", "" + userid);
            param.put("pageNo", "1");
            param.put("pageSize", "100");
            OkHttpTask.newInstance(this).post(param, OkType.form)
                    .addHeader("authorization", authorization)
                    .enqueue(new IOkTaskCallback() {
                        @Override
                        public void onSuccess(String result) {
                            if (result != null) {
                                Log.e("fzq", "-------" + result);
                                Gson gson = new Gson();
                                nursingoldrestlistbean = gson.fromJson(result,
                                        NursingOldrestListJsonBean.class);
                                if (nursingoldrestlistbean != null
                                        && nursingoldrestlistbean.getData() != null) {
                                    controllistView(nursingoldrestlistbean
                                            .getData());
                                }
                            }
                        }
                    }).start(UrlBean.NURSINGBYOLDMANURL);
        }
    }

    /**
     * 家属端搜索老人列表
     * @param
     */
    private void controlfamilylistView() {
        if (familyoldrestlistbean.getData() == null) {
            return;
        }

        MyBaseAdapter<FamilyOlderListDateJsonBean.FamilyOlderListDataBean> myBaseAdapter = new MyBaseAdapter<FamilyOlderListDateJsonBean.FamilyOlderListDataBean>(
                this, familyoldrestlistbean.getData(),
                R.layout.item_nursing_follow_older_list) {
            @Override
            public void convert(ViewHolder helper, int position, FamilyOlderListDateJsonBean.FamilyOlderListDataBean item) {
                RoundImageView imageView = helper
                        .getView(R.id.item_nursingoldrest_list_header_iv);
                nametv = helper
                        .getView(R.id.item_nursingoldrest_list_state_tv);
                phonetv = helper
                        .getView(R.id.item_nursingoldrest_list_phone_tv);

                Glide.with(FollowOldSelectActivity.this)
                        .load(item.getHeadPicture()).into(imageView);
                nametv.setText("" + item.getName());
                phonetv.setText("" + item.getPhone());
            }
        };
        lv.setAdapter(myBaseAdapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                intent = new Intent(FollowOldSelectActivity.this,
                        SuiFangDeviceActivity.class);
                intent.putExtra("isFamily", isFamily);
                intent.putExtra(
                        "olderId",
                        ""
                                + familyoldrestlistbean.getData()
                                .get(position).getId());
                intent.putExtra("deviceMacAddress", deviceMacAddress);
                startActivity(intent);
            }
        });
    }


    /**
     * 加载数据
     *
     * @param @param nursingOldrestDataBean
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-11-3
     */
    protected void controllistView(NursingOldrestDataBean nursingOldrestDataBean) {
        if (nursingOldrestDataBean.getList() == null) {
            return;
        }

        MyBaseAdapter<NursingOldrestListBean> myBaseAdapter = new MyBaseAdapter<NursingOldrestListBean>(
                this, nursingOldrestDataBean.getList(),
                R.layout.item_nursing_follow_older_list) {
            @Override
            public void convert(ViewHolder helper, int position, NursingOldrestListBean item) {
                RoundImageView imageView = helper
                        .getView(R.id.item_nursingoldrest_list_header_iv);
                nametv = helper
                        .getView(R.id.item_nursingoldrest_list_state_tv);
                phonetv = helper
                        .getView(R.id.item_nursingoldrest_list_phone_tv);

                Glide.with(FollowOldSelectActivity.this)
                        .load(item.getHeadPicture()).into(imageView);
                nametv.setText("" + item.getName());
                phonetv.setText("" + item.getPhone());
            }
        };
        lv.setAdapter(myBaseAdapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                intent = new Intent(FollowOldSelectActivity.this,
                        SuiFangDeviceActivity.class);
                intent.putExtra("isFamily", isFamily);
                intent.putExtra(
                        "olderId",
                        ""
                                + nursingoldrestlistbean.getData().getList()
                                .get(position).getId());
                intent.putExtra("deviceMacAddress", deviceMacAddress);
                startActivity(intent);
            }
        });
    }

}
